﻿// <copyright file="Logger.cs" company="Snowman Consulting">
//     Copyright (c) 2009 Snowman Consulting. All rights reserved.
// </copyright>

namespace SnowmanConsulting.ProductKeyManager.Logging
{
    using System;
    using System.Globalization;
    using LiveLabs.Logging.Logs;

    /// <summary>
    /// Logger static class
    /// </summary>
    public static class Logger
    {
        /// <summary>
        /// Log file string
        /// </summary>
        private static string logFile = "pkmlog.log";

        /// <summary>
        /// log object
        /// </summary>
        private static LiveLabs.Logging.Log log;

        /// <summary>
        /// Gets the log file.
        /// </summary>
        /// <value>The log file.</value>
        public static string LogFile
        {
            get { return Logger.logFile; }
        }

        /// <summary>
        /// Initializes the logger.
        /// </summary>
        public static void InitializeLogger()
        {
            if (log == null)
            {
                LiveLabs.Logging.Config.Configuration.DefaultLog = new FileLog(Logger.LogFile, true);
                Logger.log = LiveLabs.Logging.Config.Configuration.DefaultLog;
            }
        }

        /// <summary>
        /// Writes the line.
        /// </summary>
        /// <param name="type">The type of log item.</param>
        public static void WriteLine(LogItemType type)
        {
            Logger.WriteLine(type, string.Empty);
        }

        /// <summary>
        /// Writes the line.
        /// </summary>
        /// <param name="type">The type of log item.</param>
        /// <param name="message">The message.</param>
        public static void WriteLine(LogItemType type, string message)
        {
            Logger.InitializeLogger();

            switch (type)
            {
                case LogItemType.ApplicationStart:
                    #if DEBUG
                    Logger.log.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0}: Product Key Manager started at {1}", type, DateTime.Now));
                    #endif
                    break;
                case LogItemType.ApplicationEnd:
                    #if DEBUG
                    Logger.log.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0}: Product Key Manager shutdown at {1}", type, DateTime.Now));
                    #endif
                    break;
                default:
                    Logger.log.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0}: {1} at {2}", type, message, DateTime.Now));
                    break;
            }
        }

        /// <summary>
        /// Writes the line.
        /// </summary>
        /// <param name="type">The type of log item.</param>
        /// <param name="ex">The exception.</param>
        public static void WriteLine(LogItemType type, Exception ex)
        {
            Logger.WriteLine(type, string.Format(CultureInfo.CurrentCulture, "Exception: {0} \r\n {1}", ex.Message, ex.StackTrace));
        }
    }
}
